package com.insight.modules.system.util;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.SecureRandom;
import java.util.Base64;
import java.util.Optional;

/**
 * 登录验证码工具类
 */
public class RandImageUtil {

    public static final String key = "JEECG_LOGIN_KEY";

    /**
     * 定义图形大小
     */
    private static final int width = 105;
    /**
     * 定义图形大小
     */
    private static final int height = 35;

    /**
     * 定义干扰线数量
     */
    private static final int count = 200;

    /**
     * 干扰线的长度=1.414*lineWidth
     */
    private static final int lineWidth = 2;

    /**
     * 图片格式
     */
    private static final String IMG_FORMAT = "JPEG";

    /**
     * base64 图片前缀
     */
    private static final String BASE64_PRE = "data:image/jpg;base64,";

    /**
     * 直接通过response 返回图片
     *
     * @param response
     * @param resultCode
     * @throws IOException
     */
    public static void generate(HttpServletResponse response, String resultCode) throws IOException {
        BufferedImage image = getImageBuffer(resultCode, null, null);
        ImageIO.write(image, IMG_FORMAT, response.getOutputStream());
    }

    /**
     * 生成base64字符串
     *
     * @param resultCode
     * @return
     * @throws IOException
     */
    public static String generate(String resultCode, Integer backColor, Integer fontColor) throws IOException {
        BufferedImage image = getImageBuffer(resultCode, backColor, fontColor);

        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        //写入流中
        ImageIO.write(image, IMG_FORMAT, byteStream);
        //转换成字节
        byte[] bytes = byteStream.toByteArray();
        //转换成base64串
        String base64 = Base64.getEncoder().encodeToString(bytes).trim();
        base64 = base64.replaceAll("\n", "").replaceAll("\r", "");

        return BASE64_PRE + base64;
    }

    private static BufferedImage getImageBuffer(String resultCode, Integer backColor, Integer fontColor) {
        // 在内存中创建图象
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        // 获取图形上下文
        Graphics2D graphics = (Graphics2D) image.getGraphics();
        Color back = new Color(Optional.ofNullable(backColor).orElse(0xFF091030), true);
        graphics.setColor(back);
        graphics.fillRect(0, 0, width, height);
        graphics.drawRect(0, 0, width - 1, height - 1);
        Color font = new Color(Optional.ofNullable(fontColor).orElse(0xFFAAAAAA), true);
        final SecureRandom random = new SecureRandom();

        for (int i = 0; i < count; i++) {
            graphics.setColor(font);
            int x = random.nextInt(width - lineWidth - 1) + 1;
            int y = random.nextInt(height - lineWidth - 1) + 1;
            int xl = random.nextInt(lineWidth);
            int yl = random.nextInt(lineWidth);
            graphics.drawLine(x, y, x + xl, y + yl);
        }

        for (int i = 0; i < resultCode.length(); i++) {
            graphics.setColor(font);
            graphics.setFont(new Font("Times New Roman", Font.BOLD, 24));
            graphics.drawString(String.valueOf(resultCode.charAt(i)), (23 * i) + 8, 26);
        }

        graphics.dispose();
        return image;
    }
}
